/**
 * @file AavsoOutputWriter.cpp
 *
 * Zapis obserwacji w formacie AAVSO - implementacja.
 *
 * @author Damian Kiełbasa
 * @date 2009
 * @version 2.0.0
 * @since 2.0.0
 */

#include "AavsoOutputWriter.h"

#include "VisualObservation.h"
#include <QApplication>
#include <QSettings>

namespace Dogz
{
    /**
     * Zwraca listę obserwacji zapisaną w formacie AAVSO.
     *
     * @return tekstowa reprezentacja listy
     */
    QString AavsoOutputWriter::getOutput() const
    {
    	QSettings settings;
        // dodajemy parametry:
        QString output = QString("#TYPE=Visual\n"
                          "#OBSCODE=%1\n"
                          "#SOFTWARE=%2\n"
                          "#DELIM=,\n"
                          "#DATE=JD\n").
                          arg(settings.value("user/aavsoCode").toString()).
                          arg("DOGZ Dziennik Obserwatora Gwiazd Zmiennych v" +
                                  qApp->applicationVersion());

        foreach (const VisualObservation& obs, m_observations)
        {
        	// 1=NAME, 2=DATE, 3=MAGNITUDE, 4=COMMENTCODE, 5=COMP1, 6=COMP2, 7=CHART, 8=NOTES
        	QString magnitude = "";
        	QString commentCode = obs.getCommentCodes();
        	QString comp1 = obs.getCompStarName();
        	QString comp2 = obs.getSecondCompStarName();
        	QString chart = obs.getChartDate();
        	QString notes = obs.getNotes();

        	if (commentCode.isEmpty())
        		commentCode = "na";
        	if (comp1.isEmpty())
        	    comp1 = "na";
        	if (comp2.isEmpty())
        	    comp2 = "na";
        	if (chart.isEmpty())
        	    chart = "na";
        	if (notes.isEmpty())
        		notes = "na";

        	if (obs.isFainterThan())
        		magnitude += "<";
        	magnitude += QString("%1").arg(obs.getMagnitude());

            output += QString("%1,%2,%3,%4,%5,%6,%7,%8\n").arg(obs.getStarName()).
                arg(obs.getJd(), 0, 'f', 4).arg(magnitude).arg(commentCode).
                arg(comp1).arg(comp2).arg(chart).arg(notes);
        }

        return output;
    }
}
